<?php
/*
 * 车型库级别管理
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 0579bcc157f3d44ac4296ddc1dac8efb86a937db $
 */
defined('IN_IXDCW') or exit('Access Denied');
class AutoCategoryLevel {

	public static function get($levelid = '', $key = '') {
		$_cache = AutoCategory::getCache(AutoCategory::TYPE_LEVEL);
		if ($levelid) {
			return $key ? $_cache[$levelid][$key] : $_cache[$levelid];
		}
		return $_cache;
	}

	public static function relatives($levelid, $children = false) {
		if ($levelid) {
			$arrparentid = self::get($levelid, 'arrparentid');
			$parentids = explode(',', $arrparentid);
		} else {
			$parentids = array(0);
		}
		if ($children) {
			array_push($parentids, $levelid);
		}
		$return = array();
		foreach($parentids as $pid) {
			$return[$pid] = self::children($pid);
		}
		return $return;
	}

	public static function parentids($levelid, $withself = false){
		if ($levelid) {
			$arrparentid = self::get($levelid, 'arrparentid');
			$parentids = explode(',', $arrparentid);
		} else {
			$parentids = array(0);
		}
		if ($withself) {
			array_push($parentids, $levelid);
		}
		return $parentids;
	}

	public static function children($parentid) {
		$_cache = self::get();
		$return = array();
		foreach($_cache as $_levelid => $_level) {
			if ($parentid == $_level['parentid']) {
				$return[$_levelid] = $_level;
			}
		}
		return $return;
	}
}